<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 订单管理
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
      <div class="container" :style="isPc ?'padding:30px': 'padding:10px'">

        <el-row :gutter="0">

          <el-col :md="8" :xs="24" style="margin:3px 0">
            <el-select  v-model="query.order_type" clearable  placeholder="订单类型"  class="handle-select mr10">
              <el-option key="1" label="到店单" value="1"></el-option>
              <el-option key="2" label="商城单" value="2"></el-option>
            </el-select>
            <el-select v-model="query.status" clearable  placeholder="订单状态" class="handle-select mr10">
              <el-option key="0" label="订单打开" value="0"></el-option>
              <el-option key="1" label="订单完成" value="1"></el-option>
              <el-option key="2" label="订单关闭" value="0"></el-option>
            </el-select>
          </el-col>
          <el-col :md="10" :xs="24" style="margin:3px 0">
            <el-autocomplete
                v-model="qryStr"
                :debounce=0
                ref="selectUser"
                :fetch-suggestions="queryUserName"
                @select="confirmSelect"
                @clear="clearSelect"
                style="width: 65%;margin-right: 5px"
                placeholder="选择会员"
                prefix-icon="el-icon-user"
                clearable>
            </el-autocomplete>
            <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>

          </el-col>
        </el-row>



            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
            >
                <el-table-column v-if="isPc" prop="id" label="ID" width="55" align="center"></el-table-column>
                <el-table-column prop="user_str" label="用户"></el-table-column>
                <el-table-column prop="balance" label="订单金额"></el-table-column>
                <el-table-column prop="status" label="订单状态"></el-table-column>
                <el-table-column v-if="isPc"  prop="pay_status" label="支付状态"></el-table-column>
                <el-table-column v-if="isPc"  label="支付方式">
                  <template slot-scope="scope">
                    <span>{{parseInt(scope.row.pay_type) === 1 ? '赊账' : '现结'}}</span>
                  </template>
                </el-table-column>
<!--                <el-table-column v-if="isPc"  prop="deliver_status" label="发货状态"></el-table-column>-->
<!--                <el-table-column v-if="isPc"  prop="deliver_address" label="发货地址"></el-table-column>-->
                <el-table-column v-if="isPc" prop="create_time" label="下单时间"></el-table-column>
                <el-table-column label="操作" :width="isPc ? 180: 70" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="showDetail(scope.$index, scope.row)"
                        >详情</el-button>
                        <el-button
                                v-if="scope.row.deliver_status === '未发货'"
                                type="text"
                                icon="el-icon-right"
                                @click="doDeliver(scope.$index, scope.row)"
                        >发货</el-button>
                        <el-button
                                v-if="scope.row.status === '待收货' && scope.row.deliver_status === '已发货'"
                                type="text"
                                icon="el-icon-check"
                                @click="completeOrder(scope.$index, scope.row)"
                        >完成订单</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="prev, next, pager"
                    :current-page="query.page"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="订单详情" :visible.sync="detailVisible" :width="modalWidth">
            <div v-for="(m, k) in orderDetail">
                <span style="display:inline-block;width: 30%">{{m.goods_name}}</span>
                <el-input v-model="orderDetail[k].price" class="handle-input" style="margin: 10px 0 0 10px;width: 20%" ></el-input>
                <el-input v-model="orderDetail[k].quantity" class="handle-input" style="margin: 10px 0 0 10px;width: 15%" ></el-input>
                <el-button type="danger" icon="el-icon-edit" @click="editDetail(k)" style="margin: 10px 0 0 10px;">修改</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>

export default {
    name: 'order',
    data() {
        return {
            query: {
                order_type: '',
                status: '',
                user_id: '',
                page: 1,
                pageSize: 10
            },
            qryStr: '',
            tableData: [],
            orderDetail: [],
            detailVisible: false,
            pageTotal: 0,
            form: {},
            idx: -1,
            id: -1,
            modalWidth: '80%',
            isPc: false
        };
    },
    created() {
        if (document.body.clientWidth > 768) {
            this.modalWidth = '40%'
            this.isPc = true
        }
        this.getData();
    },
    methods: {
        getData() {
            this.http.getOrders(this.query).then(res => {
                this.tableData = res.data.list;
                this.pageTotal = res.data.total;
            });
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'page', 1);
            this.getData();
        },
        // 编辑操作
        showDetail(index, row) {
            this.detailVisible = true
            this.http.orderDetail(row.id).then(res => {
                if(res.code === 0) {
                    this.orderDetail = res.data
                }
            })
        },
        editDetail(k) {
            this.http.editOrderDetail(this.orderDetail[k].id, this.orderDetail[k]).then(res => {
                this.$message.success("修改订单详情成功")
            })
        },
        doDeliver(index, row) {
            this.http.deliver(row.id).then(res => {
                if(res.code === 0) {
                    this.$message.success("发货成功")
                    this.getData()
                }
            })
        },
        completeOrder(index, row) {
            this.http.completeOrder({order_id: row.id}).then(res => {
                if(res.code === 0) {
                    this.$message.success("操作成功")
                    this.getData()
                }
            })
        },
        // 保存编辑
        saveEdit() {
            this.detailVisible = false;
            this.$message.success(`修改第 ${this.idx + 1} 行成功`);
            this.$set(this.tableData, this.idx, this.form);
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'page', val);
            this.getData();
        },
        queryUserName(queryString, cb){
            this.http.getUsers({mobile: this.qryStr, with_coupon: 1}).then(res => {
            let ret = [];
            const users = res.data.list;
            const coupon = res.data.coupon_list;
            if (users instanceof Array) {
              users.forEach(item => {
                let _ = {};
                _.value = item.mobile + (item.username ? ' ' + item.username : '');
                _.id = item.id;
                _.money = item.money;
                _.level_txt = item.level_txt;
                _.coupon = coupon[item.id] ? coupon[item.id] : [];
                ret.push(_)
              })
            }
            cb(ret);
          });
        },
        confirmSelect(item){
          this.query.user_id = parseInt(item.id);
        },
        clearSelect(){
          this.query.user_id = '';
          this.$refs.selectUser.handleFocus()
        },
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 46%;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
